/**
 * login.js
 */

var serverUrl = "10.128.64.70:8090";

/**
 * StartContactCenterSession-初始化连接
 *
 * @param place
 * @param loginCode
 * @param queue
 * @param channel
 * @return
 */
function init(_place, _loginCode, queue, channel) {
    if('WebSocket' in window){

    }else{
        alert("您的浏览器版本太低，请升级浏览器版本！");
        return;
    }
    place = _place;
    loginCode = _loginCode;

    var obj = new Object();
    obj.operationName = "StartContactCenterSession";
    obj.place = place;
    obj.loginCode = loginCode;
    obj.queue = "";
    if(channel!=null && channel != ''){
        obj.channels = eval('('+channel+')');
    }else{
        var mycars=new Array("voice");
        obj.channels = mycars;
    }
    var json=JSON.stringify(obj);

    var result = ajaxServlet("http://"+serverUrl+"/api/v1/me", json);
    if (result == null || result == "undefined") {
        alert("初始化失败！");
        return false;
    }
    var sessionId = result.sessionId;
    alert(sessionId);
    var statusCode = result.statusCode;
    if (statusCode == 0) {
        window.location.href='index.do?sessionId='+sessionId;
    }else if(statusCode == 2) {//分机异常
        alert("话机异常，请检查话机！");
        return false;
    }
}

/**
 * AjaxServlet 处理softphone的相应请求
 *
 * @param url
 *            接口地址
 * @param prams
 *            相应的json参数
 * @return
 */
function ajaxServlet(url, prams) {
    var result;
    var statusCode = 1;
    $.ajax( {
        url : url,
        type : "POST",
        data : prams,
        dataType : "json",
        contentType : "application/json",
        async : false,
        timeout : 1000,
        error : function() {
        },
        success : function(data) {

            result = data;
        }

    });
    return result;
}